/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.action.form;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import nhadat.common.CommonConstant;
import nhadat.tindang.DientichkhuonvienType;
import nhadat.tindang.DientichxaydungType;
import nhadat.tindang.GiatienType;
import nhadat.tindang.KhuvucType;
import nhadat.tindang.TindangType;
import nhadat.tindangfull.ChieudaikhuonvienType;
import nhadat.tindangfull.ChieudaixaydungType;
import nhadat.tindangfull.ChieungangkhuonvienType;
import nhadat.tindangfull.ChieungangxaydungType;
import nhadat.tindangfull.DientichkhuonvienfullType;
import nhadat.tindangfull.DientichxaydungfullType;
import nhadat.tindangfull.GiatienfullType;
import nhadat.tindangfull.KhuvucfullType;
import nhadat.tindangfull.TindangfullType;
import nhadat.xml.StaxParser;
import nhadat.xml.XMLReportLogic;
import nhadat.xml.XMLmanipulate;
import nhadat.xml.objecttype.report.Report;
import nhadat.xml.objecttype.report.TindangmoiType;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.UserDataHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author huanht
 */
public class InsertTinDangActionForm extends org.apache.struts.action.ActionForm {

    private String tieude;
    private String loaitin;
    private String loainha;
    private String tenloaigiayto;
    private String thongtinchitiet;
    private String cautruc;
    private String solieudientichkhuonvien;
    private String dvddientichkhuonvien;
    private String solieuchieudaikhuonvien;
    private String dvdchieudaikhuonvien;
    private String solieuchieungangkhuonvien;
    private String dvdchieungangkhuonvien;
    private String solieudientichxaydung;
    private String dvddientichxaydung;
    private String solieuchieudaixaydung;
    private String dvdchieudaixaydung;
    private String solieuchieungangxaydung;
    private String dvdchieungangxaydung;
    private String diachi;
    private String tinhthanh;
    private String quanhuyen;
    private String solieugiatien;
    private String dvdgiatien;
    private FormFile theFile;

    private String dsTinDangSave;

    public String getDsTinDangSave() {
        return dsTinDangSave;
    }

    public void setDsTinDangSave(String dsTinDangSave) {
        this.dsTinDangSave = dsTinDangSave;
    }

    public String getCautruc() {
        return cautruc;
    }

    public void setCautruc(String cautruc) {
        this.cautruc = cautruc;
    }

    public String getDiachi() {
        return diachi;
    }

    public void setDiachi(String diachi) {
        this.diachi = diachi;
    }

    public String getDvdchieudaikhuonvien() {
        return dvdchieudaikhuonvien;
    }

    public void setDvdchieudaikhuonvien(String dvdchieudaikhuonvien) {
        this.dvdchieudaikhuonvien = dvdchieudaikhuonvien;
    }

    public String getDvdchieudaixaydung() {
        return dvdchieudaixaydung;
    }

    public void setDvdchieudaixaydung(String dvdchieudaixaydung) {
        this.dvdchieudaixaydung = dvdchieudaixaydung;
    }

    public String getDvdchieungangkhuonvien() {
        return dvdchieungangkhuonvien;
    }

    public void setDvdchieungangkhuonvien(String dvdchieungangkhuonvien) {
        this.dvdchieungangkhuonvien = dvdchieungangkhuonvien;
    }

    public String getDvdchieungangxaydung() {
        return dvdchieungangxaydung;
    }

    public void setDvdchieungangxaydung(String dvdchieungangxaydung) {
        this.dvdchieungangxaydung = dvdchieungangxaydung;
    }

    public String getDvddientichkhuonvien() {
        return dvddientichkhuonvien;
    }

    public void setDvddientichkhuonvien(String dvddientichkhuonvien) {
        this.dvddientichkhuonvien = dvddientichkhuonvien;
    }

    public String getDvddientichxaydung() {
        return dvddientichxaydung;
    }

    public void setDvddientichxaydung(String dvddientichxaydung) {
        this.dvddientichxaydung = dvddientichxaydung;
    }

    public String getDvdgiatien() {
        return dvdgiatien;
    }

    public void setDvdgiatien(String dvdgiatien) {
        this.dvdgiatien = dvdgiatien;
    }

    public String getSolieugiatien() {
        return solieugiatien;
    }

    public void setSolieugiatien(String solieugiatien) {
        this.solieugiatien = solieugiatien;
    }

    

    public String getLoainha() {
        return loainha;
    }

    public void setLoainha(String loainha) {
        this.loainha = loainha;
    }

    public String getLoaitin() {
        return loaitin;
    }

    public void setLoaitin(String loaitin) {
        this.loaitin = loaitin;
    }

    public String getQuanhuyen() {
        return quanhuyen;
    }

    public void setQuanhuyen(String quanhuyen) {
        this.quanhuyen = quanhuyen;
    }

    public String getSolieuchieudaikhuonvien() {
        return solieuchieudaikhuonvien;
    }

    public void setSolieuchieudaikhuonvien(String solieuchieudaikhuonvien) {
        this.solieuchieudaikhuonvien = solieuchieudaikhuonvien;
    }

    public String getSolieuchieudaixaydung() {
        return solieuchieudaixaydung;
    }

    public void setSolieuchieudaixaydung(String solieuchieudaixaydung) {
        this.solieuchieudaixaydung = solieuchieudaixaydung;
    }

    public String getSolieuchieungangkhuonvien() {
        return solieuchieungangkhuonvien;
    }

    public void setSolieuchieungangkhuonvien(String solieuchieungangkhuonvien) {
        this.solieuchieungangkhuonvien = solieuchieungangkhuonvien;
    }

    public String getSolieuchieungangxaydung() {
        return solieuchieungangxaydung;
    }

    public void setSolieuchieungangxaydung(String solieuchieungangxaydung) {
        this.solieuchieungangxaydung = solieuchieungangxaydung;
    }

    public String getSolieudientichkhuonvien() {
        return solieudientichkhuonvien;
    }

    public void setSolieudientichkhuonvien(String solieudientichkhuonvien) {
        this.solieudientichkhuonvien = solieudientichkhuonvien;
    }

    public String getSolieudientichxaydung() {
        return solieudientichxaydung;
    }

    public void setSolieudientichxaydung(String solieudientichxaydung) {
        this.solieudientichxaydung = solieudientichxaydung;
    }

    public String getTenloaigiayto() {
        return tenloaigiayto;
    }

    public void setTenloaigiayto(String tenloaigiayto) {
        this.tenloaigiayto = tenloaigiayto;
    }

    public FormFile getTheFile() {
        return theFile;
    }

    public void setTheFile(FormFile theFile) {
        this.theFile = theFile;
    }

    public String getThongtinchitiet() {
        return thongtinchitiet;
    }

    public void setThongtinchitiet(String thongtinchitiet) {
        this.thongtinchitiet = thongtinchitiet;
    }

    public String getTieude() {
        return tieude;
    }

    public void setTieude(String tieude) {
        this.tieude = tieude;
    }

    public String getTinhthanh() {
        return tinhthanh;
    }

    public void setTinhthanh(String tinhthanh) {
        this.tinhthanh = tinhthanh;
    }



    

    public void InSertToXMLFull(String imageFile, String username ) {
        TindangfullType[] dsTindang;
        
        DientichkhuonvienfullType dientichkhuonvien = new DientichkhuonvienfullType();
        ChieungangkhuonvienType chieungangkhuonvien = new ChieungangkhuonvienType();
        ChieudaikhuonvienType chieudaikhuonvien = new ChieudaikhuonvienType();
        
        DientichxaydungfullType dientichxaydung = new DientichxaydungfullType();
        ChieungangxaydungType chieungangxaydung = new ChieungangxaydungType();
        ChieudaixaydungType chieudaixaydung = new ChieudaixaydungType();

        KhuvucfullType khuvuc = new KhuvucfullType();
        GiatienfullType giatien = new GiatienfullType();

        XMLmanipulate test = new XMLmanipulate();
        StaxParser parser = new StaxParser();
        String path = getServlet().getServletContext().getRealPath("/");
        List<TindangfullType> result = parser.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
        dsTindang = new TindangfullType[result.size() + 1];
        for (int i = 0; i < result.size(); i++) {
            dsTindang[i] = result.get(i);
        }

        //khuon vien
        if (solieudientichkhuonvien.trim().equals("")) {
            dientichkhuonvien.setSolieu(0);
        } else {
            dientichkhuonvien.setSolieu(Double.parseDouble(solieudientichkhuonvien));
        }
        dientichkhuonvien.setDvddientichkhuonvien(dvddientichkhuonvien);
        
        if (solieuchieungangkhuonvien.trim().equals("")) {
            chieungangkhuonvien.setSolieu(0);
        } else {
            chieungangkhuonvien.setSolieu(Double.parseDouble(solieuchieungangkhuonvien));
        }
        chieungangkhuonvien.setDvdchieungangkhuonvien(dvdchieungangkhuonvien);



        if (solieuchieudaikhuonvien.trim().equals("")) {
            chieudaikhuonvien.setSolieu(0);
        } else {
            chieudaikhuonvien.setSolieu(Double.parseDouble(solieuchieudaikhuonvien));
        }
        chieudaikhuonvien.setDvdchieudaikhuonvien(dvdchieudaikhuonvien);




        if (solieudientichxaydung.trim().equals("")) {
            dientichxaydung.setSolieu(0);
        } else {
            dientichxaydung.setSolieu(Double.parseDouble(solieudientichxaydung));
        }
        dientichxaydung.setDvddientichxaydung(dvddientichxaydung);

        if (solieuchieungangxaydung.trim().equals("")) {
            chieungangxaydung.setSolieu(0);
        } else {
            chieungangxaydung.setSolieu(Double.parseDouble(solieuchieungangxaydung));
        }
        chieungangxaydung.setDvdchieungangxaydung(dvdchieungangxaydung);



        if (solieuchieudaixaydung.trim().equals("")) {
            chieudaixaydung.setSolieu(0);
        } else {
            chieudaixaydung.setSolieu(Double.parseDouble(solieuchieudaixaydung));
        }
        chieudaixaydung.setDvdchieudaixaydung(dvdchieudaixaydung);

        khuvuc.setQuanhuyen(quanhuyen);
        khuvuc.setTinhthanh(tinhthanh);

        if (solieugiatien.trim().equals("")) {
             giatien.setSolieu(0);
        } else {
            giatien.setSolieu(Double.parseDouble(solieugiatien));
        }
        giatien.setDvdgiatien(dvdgiatien);


        // taọ object tindangFull

        TindangfullType tin = new TindangfullType();

        int tmp = Integer.parseInt(getServlet().getServletContext().getAttribute(CommonConstant.MAX_MATINDANG_HIENTAI).toString());
        tin.setMatindang((tmp+1)+"");
        getServlet().getServletContext().setAttribute(CommonConstant.MAX_MATINDANG_HIENTAI, tmp+1);
        tin.setTenloaitin(loaitin);
        tin.setTenloainha(loainha);
        tin.setTenloaigiayto(tenloaigiayto);
        if (thongtinchitiet.trim().equals("")) {
            tin.setThongtinchitiet(" ");
        } else {
            tin.setThongtinchitiet(thongtinchitiet);
        }
        if (cautruc.trim().equals("")) {
            tin.setCautruc(" ");
        } else {
            tin.setCautruc(cautruc);
        }
        
        tin.setDientichkhuonvien(dientichkhuonvien);
        tin.setChieungangkhuonvien(chieungangkhuonvien);
        tin.setChieudaikhuonvien(chieudaikhuonvien);
        tin.setDientichxaydung(dientichxaydung);
        tin.setChieungangxaydung(chieungangxaydung);
        tin.setChieudaixaydung(chieudaixaydung);

        if (diachi.trim().equals("")) {
            tin.setDiachi(" ");
        } else{
            tin.setDiachi(diachi);
        }

        tin.setKhuvuc(khuvuc);
        
        if (imageFile.equals("")) {
            tin.setHinhanh(" ");
        } else {
            tin.setHinhanh(imageFile);
        }

        if (tieude.trim().equals("")) {
            tin.setTieudetin(" ");
        } else {
            tin.setTieudetin(tieude);
        }

        tin.setGiatien(giatien);
        tin.setNguoidangtin(username);

        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());

        tin.setNgaydangtin(dateNow);
        tin.setLancuoicapnhat(dateNow);
        tin.setSoluotxem(new BigInteger("0"));
        tin.setStatus("new");
        tin.setIsActive("True");

        dsTindang[result.size()] = tin;

        test.createXMLfromTindangFullType(dsTindang, path + "xml/TindangFull.xml");

        

    }

    public void InsertToXML(String image, String username) {
        TindangType[] dsTindang;

        DientichkhuonvienType dientichkhuonvien = new DientichkhuonvienType();

        DientichxaydungType dientichxaydung = new DientichxaydungType();

        KhuvucType khuvuc = new KhuvucType();
        GiatienType giatien = new GiatienType();

        XMLmanipulate test = new XMLmanipulate();
        StaxParser parser = new StaxParser();
        String path = getServlet().getServletContext().getRealPath("/");
        List<TindangType> result = parser.readXMLtoTindangTypebyStAX(path + "xml/Tindang.xml");
        dsTindang = new TindangType[result.size() + 1];
        for (int i = 0; i < result.size(); i++) {
            dsTindang[i] = result.get(i);
        }

        //khuon vien
        if (solieudientichkhuonvien.trim().equals("")) {
            dientichkhuonvien.setSolieukhuonvien(0);
        } else {
            dientichkhuonvien.setSolieukhuonvien(Double.parseDouble(solieudientichkhuonvien));
        }
        dientichkhuonvien.setDvddientichkhuonvien(dvddientichkhuonvien);

        




        if (solieudientichxaydung.trim().equals("")) {
            dientichxaydung.setSolieudientich(0);
        } else {
            dientichxaydung.setSolieudientich(Double.parseDouble(solieudientichxaydung));
        }
        dientichxaydung.setDvddientichxaydung(dvddientichxaydung);

       

        khuvuc.setQuanhuyen(quanhuyen);
        khuvuc.setTinhthanh(tinhthanh);

        if (solieugiatien.trim().equals("")) {
             giatien.setSolieugiatien(0);
        } else {
            giatien.setSolieugiatien(Double.parseDouble(solieugiatien));
        }
        giatien.setDvdgiatien(dvdgiatien);


        // taọ object tindangFull

        TindangType tin = new TindangType();
        int tmp = Integer.parseInt(getServlet().getServletContext().getAttribute(CommonConstant.MAX_MATINDANG_HIENTAI).toString());

        tin.setMatindang(tmp + "");
        tin.setTenloaitin(loaitin);
        tin.setTenloainha(loainha);
        tin.setTenloaigiayto(tenloaigiayto);
      
        tin.setDientichkhuonvien(dientichkhuonvien);
    
        tin.setDientichxaydung(dientichxaydung);
   
       
        tin.setKhuvuc(khuvuc);
        tin.setGiatien(giatien);
        if (image.equals("")) {
            tin.setHinhanh(" ");
        } else {
            tin.setHinhanh(image);
        }

        if (tieude.trim().equals("")) {
            tin.setTieudetin(" ");
        } else {
            tin.setTieudetin(tieude);
        }

        
        tin.setNguoidangtin(username);

        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());

        tin.setNgaydangtin(dateNow);
        tin.setLancuoicapnhat(dateNow);
        tin.setSoluotxem(new BigInteger("0"));
        tin.setStatus("new");


        dsTindang[result.size()] = tin;

        test.createXMLfromTindangType(dsTindang, path + "xml/Tindang.xml");
    }

    public void recordForReport(String username){
        TindangmoiType tin = new TindangmoiType();
        tin.setMatindang(getServlet().getServletContext().getAttribute(CommonConstant.MAX_MATINDANG_HIENTAI).toString());
        tin.setLoaitin(loaitin);
        tin.setNguoidangtin(username);
        
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        tin.setTime(dateNow);

        XMLReportLogic xmlrl = new XMLReportLogic();
        xmlrl.setContextPath(getServlet().getServletContext().getRealPath("/"));
        xmlrl.recordTinMoiDang(tin, CommonConstant.ACTION_INSERT);
    }

    public Document stringToDOM(String srcStr) {
        Document doc = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(srcStr));
            doc = builder.parse(is);
            
        } catch (SAXException ex) {
            Logger.getLogger(InsertTinDangActionForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(InsertTinDangActionForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(InsertTinDangActionForm.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return doc;
        }
    }

    /**
     *
     */
    public InsertTinDangActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }
}
